package mydemo.springcloudalibaba.provide.payment.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import mydemo.springcloudalibaba.api.common.entity.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Override
    public String getPaymentById_success(Long id) {
        return "success:" + Thread.currentThread().getName() + "   " + UUID.randomUUID();
    }

    @HystrixCommand(
            // 指定失败时执行的处理
            fallbackMethod = "getPaymentById_error_fallback",
            // 执行的条件参数，此处为超时5S
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")}
    )
    @Override
    public String getPaymentById_error(Long id) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "error:" + Thread.currentThread().getName() + "   " + UUID.randomUUID();
    }
    private String getPaymentById_error_fallback(Long id){
        return "错误，执行了fallback";
    }

}
